<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/commons/global.jsp" %>

<script type="text/javascript">
    var codeGeneratorDataGrid;
    $(function() {
        codeGeneratorDataGrid = $('#codeGeneratorDataGrid').datagrid({
            url : '${path}/generator/dataGrid',
            striped : true,
            rownumbers : true,
            pagination : true,
            singleSelect : true,
            idField : 'id',
            sortName : 'id',
            sortOrder : 'asc',
            pageSize : 20,
            pageList : [ 10, 20, 30, 40, 50, 100, 200, 300, 400, 500 ],
            columns : [ [
                {
                width : '100',
                title : 'id',
                field : 'id'
                ,hidden : true
            },
                {
                width : '100',
                title : '表名',
                field : 'name'
            },
            {
                width : '100',
                title : '是否是树表',
                field : 'tree',
                formatter : function(value, row, index) {
                	if(value==1){
                		return "是"
                	}
                	return "否";
                }
            },
                {
                width : '100',
                title : '描述',
                field : 'description'
            },
                {
                width : '100',
                title : '状态',
                field : 'status',
                formatter : function(value, row, index) {
                    switch (value) {
                    case 1:
                        return '已生成';
                    case 0:
                        return '未生成';
                    }
                }
            },
            ]],
            frozenColumns : [ [{
                field : 'action',
                title : '操作',
                width : 400,
                formatter : function(value, row, index) {
                    var str = '';
                    str += $.formatString('<a href="javascript:void(0)" class="codeGenerator-easyui-linkbutton-edit" data-options="plain:true,iconCls:\'fi-pencil icon-blue\'" onclick="editCodeGeneratorFun(\'{0}\');" >编辑</a>', row.name);
                    str += '&nbsp;&nbsp;|&nbsp;&nbsp;';
                    str += $.formatString('<a href="javascript:void(0)" class="codeGenerator-easyui-linkbutton-del" data-options="plain:true,iconCls:\'fi-x icon-red\'" onclick="deleteCodeGeneratorFun(\'{0}\',\'{1}\');" >删除</a>', row.id,row.name);
                      str += '&nbsp;&nbsp;|&nbsp;&nbsp;';
                    str += $.formatString('<a href="javascript:void(0)" class="codeGenerator-easyui-linkbutton-code" data-options="plain:true,iconCls:\'fi-magnifying-glass\'" onclick="generatorCodeFun(\'{0}\');" >生成代码</a>', row.name);
                      str += '&nbsp;&nbsp;|&nbsp;&nbsp;';
                    str += $.formatString('<a href="javascript:void(0)" class="codeGenerator-easyui-linkbutton-view" data-options="plain:true,iconCls:\'fi-annotate\'" onclick="viewCodeGeneratorFun(\'{0}\',\'{1}\');" >查看生成代码</a>', row.name,row.status);
                    return str;
                }
            } ] ],
            onLoadSuccess:function(data){
                $('.codeGenerator-easyui-linkbutton-edit').linkbutton({text:'编辑'});
                $('.codeGenerator-easyui-linkbutton-del').linkbutton({text:'删除'});
                $('.codeGenerator-easyui-linkbutton-view').linkbutton({text:'查看生成代码'});
                $('.codeGenerator-easyui-linkbutton-code').linkbutton({text:'生成代码'});
            },
            toolbar : '#codeGeneratorToolbar'
        });
    });

    function addCodeGeneratorFun() {
        parent.$.modalDialog({
            title : '添加表',
            width : '100%',
            height : '100%',
            href : '${path}/generator/generatorAdd.jsp',
            buttons : [ {
                text : '确定',
                handler : function() {
                   parent.$.modalDialog.openner_dataGrid = codeGeneratorDataGrid;//因为添加成功之后，需要刷新这个treeGrid，所以先预定义好
                   submit();
                }
            } ]
        });
    }
    function generatorCodeFun(name) {
        parent.$.modalDialog({
            title : '生成代码',
            width : '500',
            height : '300',
            href : '${ctxPath}/generator/generatorCodeType?name='+name,
            buttons : [ {
                text : '生成',
                handler : function() {
                    parent.$.modalDialog.openner_dataGrid = codeGeneratorDataGrid;//因为添加成功之后，需要刷新这个dataGrid，所以先预定义好
                    generator();
                }
            } ]
        });
    }
    function editCodeGeneratorFun(name) {
        parent.$.modalDialog({
            title : '编辑',
            width : '100%',
            height : '100%',
            href : '${ctxPath}/generator/editPage?tableName='+name,
            buttons : [ {
                text : '确定',
                handler : function() {
                    parent.$.modalDialog.openner_dataGrid = codeGeneratorDataGrid;//因为添加成功之后，需要刷新这个dataGrid，所以先预定义好
                    submit();
                }
            } ]
        });
    }

    function deleteCodeGeneratorFun(id,name) {
        if (id == undefined) {//点击右键菜单才会触发这个
            var rows = codeGeneratorDataGrid.datagrid('getSelections');
            id = rows[0].id;
        } else {//点击操作里面的删除图标会触发这个
            codeGeneratorDataGrid.datagrid('unselectAll').datagrid('uncheckAll');
        }
        parent.$.messager.confirm('询问', '您是否要删除吗？', function(b) {
            if (b) {
                progressLoad();
                $.post('${path}/generator/delete', {"id":id,"tableName":name}
                , function(result) {
                    if (result.success) {
                        parent.$.messager.alert('提示', result.msg, 'info');
                        codeGeneratorDataGrid.datagrid('reload');
                    }
                    progressClose();
                }, 'JSON');
            }
        });
    }
     function viewCodeGeneratorFun(name,status) {
        parent.$.modalDialog({
            title : '查看'+name+'表生成的代码类型',
            width : 800,
            height : 450,
            href : '${path}/generator/queryCode?tableName='+name+"&status="+status,
        });
    }

    function sync(){
    	 parent.$.modalDialog({
             title : '同步表',
             width : 800,
             height : 450,
             href : '${ctxPath}/generator/synctable.jsp',
         });
    }
</script>
<div class="easyui-layout" data-options="fit:true,border:false">
    <div data-options="region:'center',fit:true,border:false">
        <table id="codeGeneratorDataGrid" data-options="fit:true,border:false"></table>
    </div>
</div>
<div id="codeGeneratorToolbar" style="display: none;">
    <a onclick="addCodeGeneratorFun();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'fi-plus icon-green'">添加</a>
    <a onclick="sync();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'fi-share icon-green'">同步表</a>
</div>
